<template>
  <div>
    <el-card style="margin:20px;padding: 20px;">

      <div class="block">
        <el-carousel trigger="click">
          <el-carousel-item v-for="item in bannerList" :key="item.bannerId">
            <el-image :src="item.bannerUrl"/>
          </el-carousel-item>
        </el-carousel>
      </div>

      <el-row style="margin:10px;">
        <el-col>
          <el-button type="primary" calss="el-icon-upload" style="float:right" @click="addImg">添加轮播图</el-button>
        </el-col>
      </el-row>

      <el-table :data="bannerList" border stripe>
        <el-table-column label="序号" type="index" width="60px">
        </el-table-column>
        <el-table-column label="轮播图标题" prop="bannerTitle"></el-table-column>

        <el-table-column label="轮播图">
          <template slot-scope="scope">
            <el-image
                style="width: 100px; height: 100px"
                :src="scope.row.bannerUrl"
                :fit="fit"></el-image>
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="删除图片" placement="top">
              <el-button type="danger" class="el-icon-delete" circle
                         @click="removeImg(scope.row.bannerId)">
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "manage",
  created() {
    this.getBannerList()
  },
  data() {
    return {
      bannerList: [],
      url: 'https://meng-edu1010.oss-cn-beijing.aliyuncs.com/2-2.png',
    }
  },
  methods: {
    /*获取轮播图列表*/
    async getBannerList() {
      let result = await this.$http.banner.reqGetBannerList()
      if (result.code !== 200) return this.$message.error('轮播图列表获取失败')
      this.bannerList = result.data.rows
    },
    /*删除图片*/
    async removeImg(id) {
      const confrimResult = await this.$confirm('此操作将永久删除该图片是否继续?', '删除提示', {
        confirmButtonText: '确认删除',
        cancelButtonText: '取消操作',
        type: "warning"
      }).catch(err => err)
      if (confrimResult !== "confirm") return this.$message.info("删除操作已取消")
      let result = await this.$http.banner.reqRemoveBannerById(id)
      if (result.code !== 200) return this.$message.error("删除轮播图失败")
      this.$message.success("删除成功")
      this.getBannerList()
    },
    /*跳转添加图片页面*/
    async addImg(){
      this.$router.push("/banner/add")
    }
  },
}
</script>

<style scoped>
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n+1) {
  background-color: #d3dce6;
}
</style>
